﻿<r:RibbonWindow x:Class="Uhi.Applications.FileAid.View.FileAidView"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
                xmlns:mi="clr-namespace:Microsoft.Expression.Interactivity.Input;assembly=Microsoft.Expression.Interactions"
                xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
                xmlns:ui="clr-namespace:Uhi.Libraries.Windows.Interactivity;assembly=Uhi.Libraries.Windows.Interactivity"
                xmlns:uc="clr-namespace:Uhi.Libraries.Windows.Controls;assembly=Uhi.Libraries.Windows.Controls"
                xmlns:vm="clr-namespace:Uhi.Applications.FileAid.ViewModel"
                xmlns:lv="clr-namespace:Uhi.Applications.FileAid.View"
                xmlns:li="clr-namespace:Uhi.Applications.FileAid.Interactivity"
                Name="mainWindow"
                Title="{Binding Path=Filename}"
                Height="500"
                Width="500"
                AllowDrop="True">

    <Window.Resources>
        <lv:EncodingConverter x:Key="EncodingConverter" />
    </Window.Resources>
    
    <Window.DataContext>
        <vm:FileViewModel PerpetualFilename="FileAid_Settings.xml" />
    </Window.DataContext>

    <i:Interaction.Triggers>
        <mi:KeyTrigger Key="F" Modifiers="Ctrl">
            <ui:SearchTextAction Command="{Binding Path=SearchCommand}" />
        </mi:KeyTrigger>
        
        <ui:CommandLineArgsTrigger>
            <i:InvokeCommandAction Command="{Binding Path=OpenCommand}"
                                   CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ui:CommandLineArgsTrigger}, Path=Args[0]}" />
        </ui:CommandLineArgsTrigger>
        <ui:DragDropTrigger>
            <i:InvokeCommandAction Command="{Binding Path=OpenCommand}"
                                   CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ui:DragDropTrigger}, Path=DataArray[0]}" />
        </ui:DragDropTrigger>
        <ui:InteractionRequestTrigger RequestId="ShowMessage">
            <ui:ShowMessageBoxAction Message="{Binding RelativeSource={RelativeSource AncestorType=ui:InteractionRequestTrigger}, Path=EventData}" />
        </ui:InteractionRequestTrigger>
    </i:Interaction.Triggers>

    <DockPanel>
        
        <r:Ribbon DockPanel.Dock="Top">

            <r:Ribbon.ApplicationMenu>
                <r:RibbonApplicationMenu SmallImageSource="..\resource\icon\large\menu.png">
                    <r:RibbonApplicationMenuItem Header="開く"
                                                 ImageSource="..\resource\icon\large\open.png">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <ui:OpenFileAction Command="{Binding Path=OpenCommand}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </r:RibbonApplicationMenuItem>
                    <r:RibbonApplicationMenuItem Header="名前を付けて保存"
                                                 ImageSource="..\resource\icon\large\save.png">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <ui:SaveFileAction Command="{Binding Path=SaveCommand}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </r:RibbonApplicationMenuItem>
                    <r:RibbonSeparator />
                    <r:RibbonApplicationMenuItem Header="終了">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <ui:CloseWindowAction TargetWindow="{Binding ElementName=mainWindow}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </r:RibbonApplicationMenuItem>
                </r:RibbonApplicationMenu>
            </r:Ribbon.ApplicationMenu>

            <r:RibbonTab Header="ホーム">
                <r:RibbonGroup Header="ファイル">
                    <r:RibbonButton Label="開く"
                                    LargeImageSource="..\resource\icon\large\open.png"
                                    SmallImageSource="..\resource\icon\small\open.png">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <ui:OpenFileAction Command="{Binding Path=OpenCommand}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </r:RibbonButton>
                    <r:RibbonButton Label="保存"
                                    LargeImageSource="..\resource\icon\large\save.png"
                                    SmallImageSource="..\resource\icon\large\save.png">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <ui:SaveFileAction Command="{Binding Path=SaveCommand}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </r:RibbonButton>
                </r:RibbonGroup>

                <r:RibbonGroup Header="編集">
                    <r:RibbonButton Label="検索"
                                    LargeImageSource="..\resource\icon\large\search.png"
                                    SmallImageSource="..\resource\icon\large\search.png">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <ui:SearchTextAction Command="{Binding Path=SearchCommand}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </r:RibbonButton>
                </r:RibbonGroup>
                
                <r:RibbonGroup Header="書式">
                    <r:RibbonButton Label="フォント"
                                    LargeImageSource="..\resource\icon\large\font.png"
                                    SmallImageSource="..\resource\icon\large\font.png"
                                    Command="{Binding ElementName=fontSelector, Path=SelectCommand}" />
                    <StackPanel Orientation="Vertical">
                        <r:RibbonTextBox Label="レコード長"
                                         Text="{Binding Path=Length}" />
                        <r:RibbonComboBox Label="エンコード"
                                          Text="{Binding Path=Encoding, Converter={StaticResource EncodingConverter}}">
                            <r:RibbonGallery>
                                <r:RibbonGalleryCategory Header="文字コード">
                                    <r:RibbonGalleryItem Content="Shift_JIS" IsSelected="True" />
                                    <r:RibbonGalleryItem Content="Ascii" />
                                    <r:RibbonGalleryItem Content="Unicode" />
                                    <r:RibbonGalleryItem Content="UTF-7" />
                                    <r:RibbonGalleryItem Content="UTF-8" />
                                    <r:RibbonGalleryItem Content="UTF-32" />
                                    <r:RibbonGalleryItem Content="EUC-JP" />
                                    <r:RibbonGalleryItem Content="EBCDIC" />
                                </r:RibbonGalleryCategory>
                            </r:RibbonGallery>
                        </r:RibbonComboBox>
                        <r:RibbonCheckBox Label="改行"
                                          HorizontalAlignment="Left"
                                          IsChecked="{Binding Path=AcceptReturn}" />
                    </StackPanel>
                </r:RibbonGroup>
            </r:RibbonTab>

        </r:Ribbon>
        
        <Grid>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <ListView Name="listView"
                      Grid.Row="1"
                      ItemsSource="{Binding Path=Records}"
                      SelectedIndex="{Binding Path=SelectedIndex}"
                      AlternationCount="2">

                <ListView.Resources>
                    <Style TargetType="lv:TextEditor">
                        <Setter Property="Foreground" Value="Black" />
                    </Style>
                </ListView.Resources>
                
                <uc:Attached.Components>
                    <uc:FontSelector Name="fontSelector"
                                     FontFamily="{Binding Path=FontFamily}"
                                     FontSize="{Binding Path=FontSize}"
                                     FontStyle="{Binding Path=FontStyle}"
                                     FontWeight="{Binding Path=FontWeight}" />
                </uc:Attached.Components>

                <i:Interaction.Behaviors>
                    <li:ListSelectBehavior />
                </i:Interaction.Behaviors>

                <i:Interaction.Triggers>
                    <ui:InteractionRequestTrigger RequestId="SelectText">
                        <li:SelectTextAction />
                    </ui:InteractionRequestTrigger>
                </i:Interaction.Triggers>

                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        <Style.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="ItemsControl.AlternationIndex" Value="0" />
                                    <Condition Property="IsSelected" Value="False" />
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="Azure" />
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="ItemsControl.AlternationIndex" Value="0" />
                                    <Condition Property="IsSelected" Value="True" />
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="White" />
                            </MultiTrigger>
                            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                <Setter Property="Background" Value="White" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ListView.ItemContainerStyle>

                <ListView.View>
                    <GridView>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=Index}" />
                        <GridViewColumn Header="{Binding Path=ColumnPositionLabel}">
                            <GridViewColumn.HeaderContainerStyle>
                                <Style TargetType="GridViewColumnHeader">
                                    <Setter Property="HorizontalContentAlignment" Value="Left" />
                                </Style>
                            </GridViewColumn.HeaderContainerStyle>
                            <GridViewColumn.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock Margin="2,0,0,0" Text="{Binding}" />
                                </DataTemplate>
                            </GridViewColumn.HeaderTemplate>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem, AncestorLevel=1}, Path=Background}">
                                        <lv:TextEditor x:Name="StrText"
                                                       Text="{Binding Path=Record}"
                                                       HighlightedText="{Binding Path=DataContext.SearchText, ElementName=listView}"
                                                       CaretIndex="{Binding Path=CaretIndex}">
                                            <lv:TextEditor.InputMask>
                                                <lv:MaskingItem MaskPattern="[\u0000-\u001F\u0080]" MaskingChar=" " />
                                            </lv:TextEditor.InputMask>
                                        </lv:TextEditor>
                                        <lv:TextEditor x:Name="HexText"
                                                       RowCount="2"
                                                       Text="{Binding Path=HexRecord}"
                                                       CaretIndex="{Binding Path=HexCaretIndex}" />
                                    </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>

            </ListView>
            
        </Grid>
        
    </DockPanel>

</r:RibbonWindow>
